﻿using Ardalis.GuardClauses;
using CallApi;
using CallApi.Model;
using HRX.PayCenter.ApplicationCore.Configs;
using HRX.PayCenter.ApplicationCore.Exceptions;
using HRX.PayCenter.ApplicationCore.Interfaces;
using HRX.PayCenter.Contract;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace HRX.PayCenter.ExternalService.Device
{
    public class CallApiService: IDeviceApiExternalService
    {
        private readonly DeviceCallApiConfig _deviceCallApiConfig;

        public CallApiService(IOptions<DeviceCallApiConfigWarpper> options)
        {
            _deviceCallApiConfig = options?.Value.DeviceCallApiConfig;
        }

        /// <summary>
        /// 启动设备
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public bool StartDevice(string orderNo)
        {
            DeivceStartModel model = new DeivceStartModel()
            {
                OrderNo = orderNo,
                RequestTime = DateTime.Now.ToString()
            };
            var result = CallApiClient.CallStartDevice(
                _deviceCallApiConfig.Host,
                _deviceCallApiConfig.ApiSecret,
                model);
            if (result == null)
            {
                Guard.Against.BizException(ResultCode.设备回调接口响应出错, "设备响应为NULL");
            }
            if (result.Code != 1)
            {
                Guard.Against.BizException(ResultCode.设备回调接口响应出错, $"设备响应出错，返回Code:{result.Code},{result.Message}");
            }

            return true;
        }
    }
}
